

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[TOC] 一、参考资料  视频资料  二、运行环境  Windows11 Visual Studio Code v2022 Node.js v16.5.01 Vue&#x2F;cli v5.0.6  三、案例需求分析 基于上一次的学习，最终使用Vue-CLI脚手架实现的TodoList案例效果如下图所示：  Vue学习笔记（一）基于Vue2的TodoList待办事项增删查案例 | 基于Node">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象">
<meta property="og:url" content="http://example.com/2022/06/21/Vue2%20%E7%AC%94%E8%AE%B02%20%E5%9F%BA%E4%BA%8EVue2%E7%9A%84TodoList%E5%BE%85%E5%8A%9E%E4%BA%8B%E9%A1%B9%E6%A1%88%E4%BE%8B%20_%20localStorage%E6%9C%AC%E5%9C%B0%E5%AD%98%E5%82%A8%20_%20Vue2%E7%9A%84%E5%8F%91%E5%B8%83%E4%B8%8E%E8%AE%A2%E9%98%85%20_%20Vue2%E6%94%AF%E6%8C%81%E7%9A%84%E5%8A%A8%E7%94%BB%E7%B1%BB%20_JavaScript%E5%8E%9F%E5%9E%8B%E5%AF%B9%E8%B1%A1/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[TOC] 一、参考资料  视频资料  二、运行环境  Windows11 Visual Studio Code v2022 Node.js v16.5.01 Vue&#x2F;cli v5.0.6  三、案例需求分析 基于上一次的学习，最终使用Vue-CLI脚手架实现的TodoList案例效果如下图所示：  Vue学习笔记（一）基于Vue2的TodoList待办事项增删查案例 | 基于Node">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/838cf4c9c8c64ca1871481759a9b92bb.gif#pic_center#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/77faad00aa78459dadb8ef404f920c54.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/1ce928588ac44bcc8fe068f20dcd39b8.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/7aa6ce51674e4cd8a4bbe86a0f3283cb.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/82cad0e3258e4bb2bde8b9fa8369990b.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/8fc8d98d214f4e5183c3583a10ff812a.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/9d1bbeb727e14affa9eeabfa757a6fad.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/cea413b2bd364dc394bf98b1563d96c0.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a7b576eb65db4175b64e44376b2a4494.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4c6dbb554e774565869144e6b0e9544c.png">
<meta property="article:published_time" content="2022-06-21T13:54:10.000Z">
<meta property="article:modified_time" content="2022-08-22T15:49:28.254Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/838cf4c9c8c64ca1871481759a9b92bb.gif#pic_center#pic_center">
  
  
  
  <title>Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-06-21 21:54" pubdate>
          2022年6月21日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          19k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          163 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[TOC]</p>
<h1 id="一、参考资料"><a href="#一、参考资料" class="headerlink" title="一、参考资料"></a>一、参考资料</h1><hr>
<ul>
<li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Zy4y1K7SH?p=78">视频资料</a></li>
</ul>
<h1 id="二、运行环境"><a href="#二、运行环境" class="headerlink" title="二、运行环境"></a>二、运行环境</h1><hr>
<ul>
<li>Windows11</li>
<li>Visual Studio Code v2022</li>
<li>Node.js v16.5.01</li>
<li>Vue&#x2F;cli v5.0.6</li>
</ul>
<h1 id="三、案例需求分析"><a href="#三、案例需求分析" class="headerlink" title="三、案例需求分析"></a>三、案例需求分析</h1><hr>
<p>基于上一次的学习，最终使用Vue-CLI脚手架实现的TodoList案例效果如下图所示：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/125364107">Vue学习笔记（一）基于Vue2的TodoList待办事项增删查案例 | 基于Node.js 使用Vue-CLI脚手架构建项目</a></li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/838cf4c9c8c64ca1871481759a9b92bb.gif#pic_center#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>现对案例提出几个需求：</p>
<ol>
<li>确保页面刷新后，当前的待办事项数据不会丢失。</li>
<li>提供编辑功能，可以修改已经添加的待办事项。</li>
<li>为熟悉Vue2动画样式的简单使用，给TodoList添加一个动态的效果。</li>
</ol>
<p>最终实现的效果如下：</p>
<p><img src="https://img-blog.csdnimg.cn/77faad00aa78459dadb8ef404f920c54.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="四、相关知识"><a href="#四、相关知识" class="headerlink" title="四、相关知识"></a>四、相关知识</h1><hr>
<h2 id="4-1-JavaScript-原型对象"><a href="#4-1-JavaScript-原型对象" class="headerlink" title="4.1 JavaScript 原型对象"></a>4.1 JavaScript 原型对象</h2><p>参考资料：<a target="_blank" rel="noopener" href="https://www.runoob.com/js/js-object-prototype.html#:~:text=JavaScript%20prototype%EF%BC%88%E5%8E%9F%E5%9E%8B%E5%AF%B9%E8%B1%A1%EF%BC%89%20%E6%89%80%E6%9C%89%E7%9A%84,JavaScript%20%E5%AF%B9%E8%B1%A1%E9%83%BD%E4%BC%9A%E4%BB%8E%E4%B8%80%E4%B8%AA%20prototype%EF%BC%88%E5%8E%9F%E5%9E%8B%E5%AF%B9%E8%B1%A1%EF%BC%89%E4%B8%AD%E7%BB%A7%E6%89%BF%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%B9%E6%B3%95%E3%80%82">菜鸟教程 JavaScript 高阶</a></p>
<p>JavaScript中的对象构造器</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name, age</span>) &#123;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age;<br>&#125;<br><span class="hljs-keyword">var</span> me = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&quot;uni&quot;</span>,<span class="hljs-number">22</span>);<br><span class="hljs-keyword">var</span> myGirl = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&quot;who&quot;</span>, <span class="hljs-number">22</span>);<br></code></pre></td></tr></table></figure>
<p><code>在JavaScript中已存在构造器的实例对象中是不能添加新的属性</code></p>
<p>比如要添加一个属性 hobby 表示爱好</p>
<p><strong>错误示范：</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title class_">Person</span>.<span class="hljs-property">hobby</span> = <span class="hljs-string">&quot;吃饭&quot;</span>;<br></code></pre></td></tr></table></figure>
<p><strong>正确示范：</strong> （修改原先的对象声明）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name, age, hobby</span>) &#123;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">hobby</span> = hobby;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>1. prototype 继承</strong></p>
<p><code>所有的 JavaScript 对象都会从一个 prototype（原型对象）中继承属性和方法：</code></p>
<p>比如 JS 自带的一些对象：</p>
<ul>
<li>Date 对象从 Date.prototype 继承</li>
<li>Array 对象从 Array.prototype 继承</li>
</ul>
<p>同理，我们之前定义的Person对象也会从 Person.prototype 继承属性和方法。</p>
<p>所有 JavaScript 中的对象都是位于原型链顶端的 <code>Object</code> 的实例，这类似于Java语言中所有的类都是Object的子类，都有Object类的 <code>toString()</code> 方法。</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/a7566ba2fe804a8095d7cb5aeb6b5e35.png#pic_center">https://img-blog.csdnimg.cn/a7566ba2fe804a8095d7cb5aeb6b5e35.png#pic_center</a> &#x3D;60%x)</p>
<p>JavaScript 对象有一个指向一个原型对象的链。当试图访问一个对象的属性时，它不仅仅在该对象上搜寻，还会搜寻该对象的原型，以及该对象的原型的原型，依次层层向上搜索，直到找到一个名字匹配的属性或到达原型链的末尾（即Object）。</p>
<p>简单说，不管是JS内置的Date 对象, Array 对象, 还是我们自定义的 Person 对象都从 Object.prototype 继承。</p>
<p><strong>2. 通过prototype添加属性和方法</strong></p>
<p><strong>适用场景：</strong></p>
<ul>
<li>在所有已经存在的对象添加新的属性或方法</li>
<li>在对象的构造函数中添加属性或方法</li>
</ul>
<p><strong>应用案例：</strong></p>
<ul>
<li>给对象的构造函数添加新的属性<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">hobby</span>= <span class="hljs-string">&quot;吃饭&quot;</span>;<br></code></pre></td></tr></table></figure></li>
<li>给对象的构造函数添加新的方法</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">first, last, age, eyecolor</span>) &#123;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age;<br>&#125;<br> <br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">hobby</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) &#123;<br>  <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> + <span class="hljs-string">&quot;的爱好是吃饭&quot;</span>;<br>&#125;;<br></code></pre></td></tr></table></figure>

<p><strong>具体案例：</strong></p>
<p>先定义一个Person对象的构造器，只有name和age两种属性，根据构造器创建一个uni对象，当点击按钮后就对该对象的prototype原型对象添加一个新的属性和新的方法，然后在通过alert打印出来：</p>
<p>具体实现代码的如下（文件格式为.html）：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>测试内容<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;test(this)&quot;</span>&gt;</span>初始化对象<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">            <span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name, age</span>)&#123;</span><br><span class="language-javascript">                <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name</span><br><span class="language-javascript">                <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age</span><br><span class="language-javascript">            &#125;</span><br><span class="language-javascript">            <span class="hljs-keyword">let</span> uni = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;uni&#x27;</span>, <span class="hljs-number">22</span>)</span><br><span class="language-javascript">            <span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">btn</span>)&#123;</span><br><span class="language-javascript">                <span class="hljs-comment">// 获取上一级的h1标签</span></span><br><span class="language-javascript">                <span class="hljs-keyword">let</span> h1 = btn.<span class="hljs-property">previousElementSibling</span></span><br><span class="language-javascript">                <span class="hljs-comment">// 通过prototype添加属性</span></span><br><span class="language-javascript">                <span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">hobby</span> = <span class="hljs-string">&quot;吃饭&quot;</span></span><br><span class="language-javascript">                <span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">showName</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">                    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;我是: &quot;</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> </span><br><span class="language-javascript">                &#125;</span><br><span class="language-javascript">                <span class="hljs-comment">// 返回JSON格式的测试内容</span></span><br><span class="language-javascript">                h1.<span class="hljs-property">innerHTML</span> = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(uni)</span><br><span class="language-javascript">                <span class="hljs-comment">// 将对象打印到控制台</span></span><br><span class="language-javascript">                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uni)</span><br><span class="language-javascript">                <span class="hljs-comment">// 通过对象访问原型的属性</span></span><br><span class="language-javascript">                <span class="hljs-title function_">alert</span>(uni.<span class="hljs-title function_">showName</span>(), uni.<span class="hljs-property">hobby</span>)</span><br><span class="language-javascript">            &#125;</span><br><span class="language-javascript">        </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><strong>运行效果：</strong></p>
<p>点击初始化对象的按钮，弹窗显示了对象通过prototype设置的方法和属性<br><img src="https://img-blog.csdnimg.cn/1ce928588ac44bcc8fe068f20dcd39b8.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>点击确定后，对象的内容将以JSON格式显示在 h1 标题，如下图所示<br><img src="https://img-blog.csdnimg.cn/7aa6ce51674e4cd8a4bbe86a0f3283cb.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-2-Vue的生命周期"><a href="#4-2-Vue的生命周期" class="headerlink" title="4.2 Vue的生命周期"></a>4.2 Vue的生命周期</h2><p>根据Vue官方介绍的：<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/guide/instance.html">查看</a></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs">每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如，需要设置数据监听、编译模板、将实例挂载到 DOM 并在数据变化时更新 DOM 等。<br><br>同时在这个过程中也会运行一些叫做生命周期钩子的函数，这给了用户在不同阶段添加自己的代码的机会。<br></code></pre></td></tr></table></figure>

<p>在英文版中可能描述的会更详细一点：<a target="_blank" rel="noopener" href="https://vuejs.org/guide/essentials/lifecycle.html">查看</a></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs">每个Vue组件实例在创建时都会经历一系列初始化步骤<br><br>例如，它需要设置数据观察、编译模板、将实例装载到DOM，以及在数据更改时更新DOM。<br><br>同时，它还运行称为生命周期挂钩的函数，使用户有机会在特定阶段添加自己的代码。<br></code></pre></td></tr></table></figure>

<p>Vue的生命周期如下图所示：<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/db0ac612da184b1b886e5f89c580dbb4.png#pic_center">https://img-blog.csdnimg.cn/db0ac612da184b1b886e5f89c580dbb4.png#pic_center</a> &#x3D;60%x)<br>根据官方介绍，生命周期钩子（Hooks）就是上图中的红线圆框所表示的函数，本质上就是会在指定阶段执行的函数。</p>
<p>在实现这个案例时，我们主要用到三个钩子函数，分别是</p>
<ul>
<li>beforeCreate() ，Vue中数据代理和数据劫持创建前</li>
<li>monted()，即DOM元素挂载后</li>
<li>beforeDestory()，Vue组件销毁前</li>
</ul>
<h3 id="4-2-1-beforeCreate-amp-全局事件总线"><a href="#4-2-1-beforeCreate-amp-全局事件总线" class="headerlink" title="4.2.1 beforeCreate()  &amp; 全局事件总线"></a>4.2.1 beforeCreate()  &amp; 全局事件总线</h3><p>参考资料：<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Zy4y1K7SH?p=84">视频资料</a></p>
<p>在之前我们通过vue创建了多个组件，各个组件有上下层调用的关系，那么根据新的需求，现在就存在组件之间的数据通信问题，比如在点击编辑过后，TodoItem组件需要将编辑后的信息传递给App组件，因为数据是存储在App中的，为了维持Vue提供的MVVM模型，我们尽量不在被引入的子组件中直接去修改从父组件App里props传递的数据。<br><img src="https://img-blog.csdnimg.cn/82cad0e3258e4bb2bde8b9fa8369990b.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>Vue的每个组件都是VueComponent的实例对象，根据原型链的知识，它们的<code>__proto__</code>属性是指向Vue原型对象的，这样就可以做到Vue之间的多个组件数据共享。</p>
<p>（下图摘自视频资料）<br><img src="https://img-blog.csdnimg.cn/8fc8d98d214f4e5183c3583a10ff812a.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>图中比较重要的等式：</p>
<p><code>VueComponent.prototype.__proto__  === Vue.prototype</code> 恒等于 $true$</p>
<p>注意：这里的VueComponent和Vue都是原型对象，而不是实例对象。</p>
<p>数据通信核心思路：提供一个Vue组件，存入Vue.prototype中专门用于数据通信。</p>
<p>在引入Vue的JS代码文件中进行配置</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vue</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">App</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./App.vue&#x27;</span><br><br><span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span><br><br><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>  <span class="hljs-attr">render</span>: <span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> <span class="hljs-title function_">h</span>(<span class="hljs-title class_">App</span>),<br>  <span class="hljs-title function_">beforeCreate</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-title class_">Vue</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">$bus</span> = <span class="hljs-variable language_">this</span><br>  &#125;<br>&#125;).$mount(<span class="hljs-string">&#x27;#app&#x27;</span>)<br></code></pre></td></tr></table></figure>

<p>这里我们通过 beforeCreate() 钩子来定义Vue的原型变量，<code>$bus</code>，值就为Vue实例对象本身，这样的话，Vue内部的Vue Component组件实例对象就可以访问到 <code>$bus</code> ，这里的 <code>$bus</code> 称作全局事件的总线。</p>
<p>接下来总结以下全局事件总线的用法：</p>
<ol>
<li>全局事件总线是一种Vue2组件之间的通信方式，适用于<strong>任意</strong>组件之间通信</li>
<li>配置全局事件总线<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>	...<br>	<span class="hljs-comment">// 在钩子函数中定义</span><br>	<span class="hljs-title function_">beforeCreate</span>(<span class="hljs-params"></span>)&#123;<br>		<span class="hljs-title class_">Vue</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">$bus</span> = <span class="hljs-variable language_">this</span><br>	&#125;,<br>&#125;)<br></code></pre></td></tr></table></figure></li>
<li>使用事件总线</li>
</ol>
<ul>
<li><p>接收数据：A组件想接收数据，则在A组件中给<code>$bus$</code> 绑定自定义事件，事件的回调留在A组件自身。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">methods</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-title function_">update</span>(<span class="hljs-params">data</span>) &#123; ... &#125; <br>&#125;<br>...<br><span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$on(<span class="hljs-string">&#x27;xxxx&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">update</span>)<br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li><p>提供数据</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$emit(<span class="hljs-string">&#x27;xxx&#x27;</span>, 数据)<br></code></pre></td></tr></table></figure></li>
</ul>
<p>Vue2的<code>$emit</code>和<code>$on</code>接下来会做补充。</p>
<h2 id="4-3-消息发布和消息订阅-amp-emit-和-on"><a href="#4-3-消息发布和消息订阅-amp-emit-和-on" class="headerlink" title="4.3 消息发布和消息订阅  &amp; $emit 和 $on"></a>4.3 消息发布和消息订阅  &amp; $emit 和 $on</h2><p>我们可以使用现有的JS插件，比如pubsub来实现这种消息发布与订阅。</p>
<p>在Vue2的各组件数据通信中，为了确保数据能顺利传送 ，通常两个组件之间需要提前对消息进行定义。</p>
<p>所谓的消息通常就是指函数，一方负责定义接收的函数参数以及函数的实现逻辑（消息订阅者），另一方则负责指定调用的函数，以及传入的数据（消息发布者）。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 安装发布与订阅插件 : npm i pubsub-js</span><br><span class="hljs-comment">// 订阅消息(返回一个消息ID)</span><br><span class="hljs-keyword">const</span> pubsubId = pubsub.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">&#x27;消息名称&#x27;</span>, 回调函数(消息名, 消息参数))<br><span class="hljs-comment">// 发布消息</span><br>pubsub.<span class="hljs-title function_">publish</span>(<span class="hljs-string">&#x27;消息名称&#x27;</span>, 消息参数)<br><span class="hljs-comment">// 取消订阅(根据发布消息的ID)</span><br>pubsub.<span class="hljs-title function_">unsubscribe</span>(pubsubId) <br></code></pre></td></tr></table></figure>

<p>除了使用 <code>pubsub.js</code>  插件以外，我们还可以通过 Vue2 的 $emit 和 $on来实现</p>
<p><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#vm-on"><strong>$on</strong></a></p>
<p><code>vm.$on( event, callback )</code></p>
<p>参数：</p>
<ul>
<li>{string | Array<string>} event (数组只在 2.2.0+ 中支持)</li>
<li>{Function} callback</li>
</ul>
<p>作用：</p>
<p>监听当前实例上的自定义事件。事件可以由 vm.$emit 触发。回调函数会接收所有传入事件触发函数的额外参数。</p>
<p>示例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 订阅消息</span><br>vm.$on(<span class="hljs-string">&#x27;test&#x27;</span>, <span class="hljs-keyword">function</span> (<span class="hljs-params">msg</span>) &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(msg)<br>&#125;)<br><span class="hljs-comment">// 发布消息</span><br>vm.$emit(<span class="hljs-string">&#x27;test&#x27;</span>, <span class="hljs-string">&#x27;hi&#x27;</span>)<br><span class="hljs-comment">// =&gt; &quot;hi&quot;</span><br></code></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#vm-emit"><strong>$emit</strong></a></p>
<p><code>vm.$emit( eventName, […args] )</code></p>
<p>参数：</p>
<ul>
<li>{string} eventName</li>
<li>[…args]</li>
</ul>
<p>作用：触发当前实例上的事件。附加参数都会传给监听器回调。</p>
<p>除了绑定事件 <code>$on</code> 以外，还有解绑事件 <code>$off</code> </p>
<p><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#vm-off"><strong>$off</strong></a></p>
<p><code>vm.$off( [event, callback] )</code><br>参数：</p>
<ul>
<li>{string | Array<string>} event (只在 2.2.2+ 支持数组)</li>
<li>{Function} [callback]</li>
</ul>
<p><strong>用法：</strong></p>
<ul>
<li><p>移除自定义事件监听器</p>
</li>
<li><p>如果没有提供参数，则移除<strong>所有</strong>的事件监听器</p>
</li>
<li><p>如果只提供了事件，则移除该事件<strong>所有</strong>的监听器</p>
</li>
<li><p>如果同时提供了事件与回调，则只移除这个回调的监听器。</p>
</li>
</ul>
<p>这里又要用到之前提到的全局事件总线的思路了，之前我们是通过 <code>props</code> 来传参的，通常是用于接收父组件传过来的变量或者函数，在有了全局总线过后，可以直接通过VueComponent组件对象里的原型来获取到 <code>$bus</code> ，所有的组件对象都能获取到 <code>$bus</code>的值，那么就能通过它订阅或者发布消息了。</p>
<p>这里的<code>$bus</code>本质上就是 Vue对象，因为Vue组件之间的原型都是指向Vue实例的，而Vue对象也指向其Vue实例，所以同一个Vue对象引入的Vue组件都可以通过它来进行数据通信。</p>
<h2 id="4-4-Vue2动画"><a href="#4-4-Vue2动画" class="headerlink" title="4.4 Vue2动画"></a>4.4 Vue2动画</h2><h3 id="4-4-1-内置组件-transition"><a href="#4-4-1-内置组件-transition" class="headerlink" title="4.4.1 内置组件 transition"></a>4.4.1 内置组件 transition</h3><p><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#transition"><strong>transition</strong></a>    是 Vue2内置的组件，它通常用来设置单个HTML标签的动画，多个标签动画则需要使用  <a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#transition-group"><strong>transition-group</strong></a></p>
<p>作用：</p>
<p><code>&lt;transition&gt;</code> 元素作为单个元素&#x2F;组件的过渡效果。<code>&lt;transition&gt; </code>只会把过渡效果应用到其包裹的内容上，而不会额外渲染 DOM 元素，也不会出现在可被检查的组件层级中。</p>
<p><strong>官方提供的案例：</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 简单元素 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">transition</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;ok&quot;</span>&gt;</span>toggled content<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">transition</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- 动态组件 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">transition</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;fade&quot;</span> <span class="hljs-attr">mode</span>=<span class="hljs-string">&quot;out-in&quot;</span> <span class="hljs-attr">appear</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">component</span> <span class="hljs-attr">:is</span>=<span class="hljs-string">&quot;view&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">component</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">transition</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- 事件钩子 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;transition-demo&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">transition</span> @<span class="hljs-attr">after-enter</span>=<span class="hljs-string">&quot;transitionComplete&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;ok&quot;</span>&gt;</span>toggled content<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">transition</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;</span><br><span class="language-javascript">  ...</span><br><span class="language-javascript">  <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">transitionComplete</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">el</span>) &#123;</span><br><span class="language-javascript">      <span class="hljs-comment">// 传入 &#x27;el&#x27; 这个 DOM 元素作为参数。</span></span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">  &#125;</span><br><span class="language-javascript">  ...</span><br><span class="language-javascript">&#125;).$mount(<span class="hljs-string">&#x27;#transition-demo&#x27;</span>)</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>在本次案例中用到的部分：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">transition</span> <span class="hljs-attr">appear</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">input</span></span><br><span class="hljs-tag">                <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;edit&quot;</span></span><br><span class="hljs-tag">                <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;todoObj.isEdit&quot;</span> </span><br><span class="hljs-tag">                <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;inputTodo&quot;</span></span><br><span class="hljs-tag">                <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;come&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">transition</span>&gt;</span><br>        <br>  <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">&#x27;animate.css&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoItem&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">        ...</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 引入TodoBody传来的todoObj</span></span><br><span class="language-javascript">    <span class="hljs-attr">props</span>: [<span class="hljs-string">&#x27;todoObj&#x27;</span>],</span><br><span class="language-javascript"></span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-class">.come</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">animation</span>: fromRight linear <span class="hljs-number">1s</span>;</span><br><span class="language-css">    &#125;    </span><br><span class="language-css">    <span class="hljs-selector-class">.to</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">animation</span>: fromRight linear <span class="hljs-number">1s</span> reverse;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-comment">/* Vue自动指定name为edit和flag的进入和进出样式 */</span></span><br><span class="language-css">    <span class="hljs-selector-class">.edit-enter-active</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">animation</span>: fromRight linear <span class="hljs-number">1s</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.edit-leave-active</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">animation</span>: fromRight linear <span class="hljs-number">1s</span> reverse;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-comment">/* Vue 动画 */</span></span><br><span class="language-css">    <span class="hljs-keyword">@keyframes</span> fromRight&#123;</span><br><span class="language-css">        <span class="hljs-selector-tag">from</span>&#123;</span><br><span class="language-css">            <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(<span class="hljs-number">100px</span>);</span><br><span class="language-css">            <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0</span>;</span><br><span class="language-css">        &#125;</span><br><span class="language-css">        <span class="hljs-selector-tag">to</span>&#123;</span><br><span class="language-css">            <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(<span class="hljs-number">0px</span>);</span><br><span class="language-css">            <span class="hljs-attribute">opacity</span>: <span class="hljs-number">100</span>;</span><br><span class="language-css">        &#125;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>上述案例中的 input 标签只有在 todoObj.isEdit 值为 true时才显示，为了方便查看Vue2内置的transition组件的使用，我把其他关系不大的内容都忽略了，这里可以看到 <code>-enter-active</code> 和 <code>-leave-active</code> 是固定的一种类名，分别表示进入和离开时加载的动画，除了这两个以外，还有 其他的比如：</p>
<ul>
<li>v-enter</li>
<li>v-leave </li>
<li>v-appear</li>
<li>v-enter-to</li>
<li>v-leave-to</li>
<li>v-appear-to</li>
<li>v-enter-active</li>
<li>v-leave-active</li>
<li>v-appear-active</li>
<li>其中 v 可以改成 <code>&lt;transition&gt;</code> 标签内的name属性，比如上面的案例中，就定位到了name&#x3D;”edit”的标签。</li>
</ul>
<h3 id="4-4-2-transition-group"><a href="#4-4-2-transition-group" class="headerlink" title="4.4.2 transition-group"></a>4.4.2 transition-group</h3><p><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#transition-group"><strong>transition-group</strong></a> 是上一个组件的升级版，它可以同时支持设置多个组件的动画。</p>
<p>它和 <code>transition</code> 最大的区别就是，该标签里的所有HTML标签 <strong>都需要指定 key</strong> 。</p>
<p><transition-group> 元素作为多个元素&#x2F;组件的过渡效果。<transition-group> 渲染一个真实的 DOM 元素。默认渲染 <span>，可以通过 tag attribute 配置哪个元素应该被渲染。</p>
<p>官方案例（这里很明显，它比较适合设置列表、表格之类的样式）：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">transition-group</span> <span class="hljs-attr">tag</span>=<span class="hljs-string">&quot;ul&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;slide&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;item in items&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;item.id&quot;</span>&gt;</span><br>    &#123;&#123; item.text &#125;&#125;<br>  <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">transition-group</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="4-5-第三方CSS动画库-animate-css"><a href="#4-5-第三方CSS动画库-animate-css" class="headerlink" title="4.5 第三方CSS动画库 animate.css"></a>4.5 第三方CSS动画库 animate.css</h2><p>安装的命令</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs stylus">npm install animate<span class="hljs-selector-class">.css</span> <span class="hljs-attr">--save</span><br></code></pre></td></tr></table></figure>

<p>使用：在任意组件中的 <code>&lt;script&gt;</code> 中导入css</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">&#x27;animate.css&#x27;</span><br></code></pre></td></tr></table></figure>

<p>定义在动画标签：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">transition</span> </span><br><span class="hljs-tag">    <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;animate__animated anmate__bounce&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">enter-active-class</span>=<span class="hljs-string">&quot;animate__backInDown&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">leave-active-class</span>=<span class="hljs-string">&quot;animate__backOutUp&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">appear</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;./assets/logo.png&quot;</span>&gt;</span>基于Vue2的TodoList案例<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TodoHeader</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoHeader</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TodoBody</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoBody</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TodoFooter</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoFooter</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">transition</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoHeader</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/TodoHeader.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoBody</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/TodoBody.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoFooter</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/TodoFooter.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">&#x27;animate.css&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;App&#x27;</span>,</span><br><span class="language-javascript">  <span class="hljs-attr">components</span>: &#123;<span class="hljs-title class_">TodoHeader</span>, <span class="hljs-title class_">TodoBody</span>, <span class="hljs-title class_">TodoFooter</span>&#125;,</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">  <span class="hljs-selector-tag">h1</span>&#123; <span class="hljs-attribute">text-align</span>: center;&#125;</span><br><span class="language-css">  <span class="hljs-selector-tag">img</span>&#123; <span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>; <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;&#125;</span><br><span class="language-css">  <span class="hljs-selector-id">#app</span>&#123;</span><br><span class="language-css">    <span class="hljs-attribute">width</span>: <span class="hljs-number">600px</span>;</span><br><span class="language-css">    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;</span><br><span class="language-css">  &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>关于样式的配置同样是放在 <code>&lt;transition&gt;</code>标签上，主要就是以下三句：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs javascript">name=<span class="hljs-string">&quot;animate__animated anmate__bounce&quot;</span><br>enter-active-<span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;animate__backInDown&quot;</span><br>leave-active-<span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;animate__backOutUp&quot;</span><br></code></pre></td></tr></table></figure>
<p>首先name值是固定的，根据这个插件的特点，只有对标签设置固定的name，配置animate.css相关的动画才会有效。</p>
<p>前面的 enter-active-class 这个其实是Vue2提供的属性，毕竟 <code>&lt;transition&gt;</code> 是Vue2内置的一个组件，更多的属性可参考：<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#transition">查看</a>，后面的类名就是插件提供的，可以在官网找（官网的效果很好，点击后有动画）：</p>
<p><a target="_blank" rel="noopener" href="https://animate.style/">https://animate.style/</a></p>
<p><img src="https://img-blog.csdnimg.cn/9d1bbeb727e14affa9eeabfa757a6fad.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-6-HTML5-存储"><a href="#4-6-HTML5-存储" class="headerlink" title="4.6 HTML5 存储"></a>4.6 HTML5 存储</h2><p><a target="_blank" rel="noopener" href="https://www.runoob.com/html/html5-webstorage.html"><strong>HTML5 webstorage</strong></a></p>
<p>HTML5 web 存储，一个比 cookie 更好的本地存储方式。</p>
<p>浏览器存储数据的两个对象为：</p>
<ul>
<li><p>localStorage - 用于长久保存整个网站的数据，保存的数据没有过期时间，直到手动去除。</p>
</li>
<li><p>sessionStorage - 用于临时保存同一窗口(或标签页)的数据，在关闭窗口或标签页之后将会删除这些数据。</p>
</li>
</ul>
<p>我们在浏览网页时，大部分网站都会保留一些信息到我们的浏览器当中。比如访问 <a target="_blank" rel="noopener" href="https://www.baidu.com/">Baidu</a><br><img src="https://img-blog.csdnimg.cn/cea413b2bd364dc394bf98b1563d96c0.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>这里可以看到，当我想搜索一些内容时，它会提示搜索历史，那么这个搜索历史是存储在哪里的呢？</p>
<p>是 Cookie 还是 Session ？ </p>
<p>其实二者都不是，而是存储在HTML5 支持的 localStorage对象中。</p>
<p>我们可以通过浏览器调试工具直接查看：</p>
<p><img src="https://img-blog.csdnimg.cn/a7b576eb65db4175b64e44376b2a4494.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>可以通过 JavaScript 来设置数据存储，这样就能记录用户的一些输入信息，比较适合搜索引擎。</p>
<p>在使用 web 存储前,应检查浏览器是否支持 localStorage 和 sessionStorage</p>
<p>（这两个都是JS的内置对象，在JS代码中可以直接调用）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">if</span>(<span class="hljs-title function_">typeof</span>(<span class="hljs-title class_">Storage</span>)!==<span class="hljs-string">&quot;undefined&quot;</span>)<br>&#123;<br>    <span class="hljs-comment">// 是的! 支持 localStorage  sessionStorage 对象!</span><br>    <span class="hljs-comment">// 一些代码.....</span><br>&#125; <span class="hljs-keyword">else</span> &#123;<br>    <span class="hljs-comment">// 抱歉! 不支持 web 存储。</span><br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>localStorage</code> 常用命令（sessionStorage类似）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 存储</span><br><span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">setItem</span>(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;uni&quot;</span>);<br><span class="hljs-comment">// 获取</span><br><span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&quot;name&quot;</span>);<br><span class="hljs-comment">// 删除单个</span><br><span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">removeItem</span>(<span class="hljs-string">&quot;name&quot;</span>);<br><span class="hljs-comment">// 删除所有</span><br><span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">clear</span>()<br><span class="hljs-comment">// 得到某个索引的Key</span><br><span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">key</span>(index);<br></code></pre></td></tr></table></figure>

<p><strong>提示:</strong> localStorage 中的键&#x2F;值对通常以字符串存储，可以按自己的需要转换该格式。</p>
<p>对象存储可以通过JSON类型来格式化，将所有的待办事项（Array类型）通过JS存储到 <code>localStorage</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 存Array</span><br><span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">setItem</span>(<span class="hljs-string">&quot;todoList&quot;</span>, <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(todoList));<br><span class="hljs-comment">// 读取Array</span><br><span class="hljs-keyword">const</span> todoList = <span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&quot;todoList, JSON.parse(todoList));</span><br></code></pre></td></tr></table></figure>

<h1 id="五、实现代码"><a href="#五、实现代码" class="headerlink" title="五、实现代码"></a>五、实现代码</h1><hr>
<p>项目结构：<br><img src="https://img-blog.csdnimg.cn/4c6dbb554e774565869144e6b0e9544c.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="5-1-vue-config-js-项目配置"><a href="#5-1-vue-config-js-项目配置" class="headerlink" title="5.1 vue.config.js 项目配置"></a>5.1 vue.config.js 项目配置</h2><p>脚手架默认生成的配置，没有变动</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123; defineConfig &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@vue/cli-service&#x27;</span>)<br><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-title function_">defineConfig</span>(&#123;<br>	<span class="hljs-attr">transpileDependencies</span>: <span class="hljs-literal">true</span>,<br>&#125;)<br></code></pre></td></tr></table></figure>
<h2 id="5-2-main-js"><a href="#5-2-main-js" class="headerlink" title="5.2 main.js"></a>5.2 main.js</h2><p>程序的入口文件，该构建Vue实例，同时指定组件相互通信的 <code>$bus</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vue</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">App</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./App.vue&#x27;</span><br><br><span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span><br><br><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>  <span class="hljs-attr">render</span>: <span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> <span class="hljs-title function_">h</span>(<span class="hljs-title class_">App</span>),<br>  <span class="hljs-title function_">beforeCreate</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-title class_">Vue</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">$bus</span> = <span class="hljs-variable language_">this</span><br>  &#125;<br>&#125;).$mount(<span class="hljs-string">&#x27;#app&#x27;</span>)<br><br></code></pre></td></tr></table></figure>
<h2 id="5-3-App-vue"><a href="#5-3-App-vue" class="headerlink" title="5.3 App.vue"></a>5.3 App.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">transition</span> </span><br><span class="hljs-tag">    <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;animate__animated anmate__bounce&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">enter-active-class</span>=<span class="hljs-string">&quot;animate__backInDown&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">leave-active-class</span>=<span class="hljs-string">&quot;animate__backOutUp&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">appear</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;./assets/logo.png&quot;</span>&gt;</span>基于Vue2的TodoList案例<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TodoHeader</span> @<span class="hljs-attr">fnAddTodo</span>=<span class="hljs-string">&quot;fnAddTodo&quot;</span> &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoHeader</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TodoBody</span> <span class="hljs-attr">:todoList</span>=<span class="hljs-string">&quot;todoList&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoBody</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TodoFooter</span> <span class="hljs-attr">:todoList</span>=<span class="hljs-string">&quot;todoList&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoFooter</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">transition</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoHeader</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/TodoHeader.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoBody</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/TodoBody.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoFooter</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/TodoFooter.vue&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;App&#x27;</span>,</span><br><span class="language-javascript">  <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">    <span class="hljs-keyword">return</span> &#123;</span><br><span class="language-javascript">      <span class="hljs-attr">todoList</span>: <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(<span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&#x27;todoList&#x27;</span>)) || []</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">  &#125;,</span><br><span class="language-javascript">  <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 添加新的待办事项</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnAddTodo</span>(<span class="hljs-params">todoObj</span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">unshift</span>(todoObj)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 切换待办事项的状态</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnCheckTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">todoObj</span>) =&gt;</span> &#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">if</span>(id === todoObj.<span class="hljs-property">id</span>)</span><br><span class="language-javascript">          todoObj.<span class="hljs-property">done</span> = !todoObj.<span class="hljs-property">done</span></span><br><span class="language-javascript">      &#125;)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 删除待办事项</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnDeleteTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">filter</span>(<span class="hljs-function"><span class="hljs-params">todoObj</span> =&gt;</span> todoObj.<span class="hljs-property">id</span> !== id)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 批量操作待办事项的状态</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnUpdateCheckTodo</span>(<span class="hljs-params">done</span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">todoObj</span>) =&gt;</span> todoObj.<span class="hljs-property">done</span> = done)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 清除已完成的待办事项</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnClearFinishedTodo</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-keyword">if</span>(<span class="hljs-title function_">confirm</span>(<span class="hljs-string">&#x27;确认要删除吗?&#x27;</span>))</span><br><span class="language-javascript">        <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">todoObj</span>) =&gt;</span> !todoObj.<span class="hljs-property">done</span>)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 更新待办事项的内容</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnUpdateTodo</span>(<span class="hljs-params">id, title</span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">todoObj</span>) =&gt;</span> &#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">if</span>(todoObj.<span class="hljs-property">id</span> === id)</span><br><span class="language-javascript">          todoObj.<span class="hljs-property">title</span> = title</span><br><span class="language-javascript">      &#125;)</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">  &#125;,</span><br><span class="language-javascript">  <span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$on(<span class="hljs-string">&#x27;fnCheckTodo&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">fnCheckTodo</span>)</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$on(<span class="hljs-string">&#x27;fnUpdateTodo&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">fnUpdateTodo</span>)</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$on(<span class="hljs-string">&#x27;fnDeleteTodo&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">fnDeleteTodo</span>)</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$on(<span class="hljs-string">&#x27;fnClearFinishedTodo&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">fnClearFinishedTodo</span>)</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$on(<span class="hljs-string">&#x27;fnUpdateCheckTodo&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">fnUpdateCheckTodo</span>)</span><br><span class="language-javascript">  &#125;,</span><br><span class="language-javascript">  <span class="hljs-title function_">beforeDestroy</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$off(<span class="hljs-string">&#x27;fnCheckTodo&#x27;</span>)</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$off(<span class="hljs-string">&#x27;fnUpdateTodo&#x27;</span>)</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$off(<span class="hljs-string">&#x27;fnDeleteTodo&#x27;</span>)</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$off(<span class="hljs-string">&#x27;fnUpdateTodo&#x27;</span>)</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$off(<span class="hljs-string">&#x27;fnClearFinishedTodo&#x27;</span>)</span><br><span class="language-javascript">    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$off(<span class="hljs-string">&#x27;fnUpdateCheckTodo&#x27;</span>)</span><br><span class="language-javascript">  &#125;,</span><br><span class="language-javascript">  <span class="hljs-attr">components</span>: &#123;<span class="hljs-title class_">TodoHeader</span>, <span class="hljs-title class_">TodoBody</span>, <span class="hljs-title class_">TodoFooter</span>&#125;,</span><br><span class="language-javascript">  <span class="hljs-comment">// 监视属性值, 若有修改则将其放到本地缓存</span></span><br><span class="language-javascript">  <span class="hljs-attr">watch</span>: &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">todoList</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-attr">deep</span>: <span class="hljs-literal">true</span>,</span><br><span class="language-javascript">      <span class="hljs-title function_">handler</span>(<span class="hljs-params">value</span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">setItem</span>(<span class="hljs-string">&#x27;todoList&#x27;</span>, <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(value))</span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">  &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">  <span class="hljs-selector-tag">h1</span>&#123; <span class="hljs-attribute">text-align</span>: center;&#125;</span><br><span class="language-css">  <span class="hljs-selector-tag">img</span>&#123; <span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>; <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;&#125;</span><br><span class="language-css">  <span class="hljs-selector-id">#app</span>&#123;</span><br><span class="language-css">    <span class="hljs-attribute">width</span>: <span class="hljs-number">600px</span>;</span><br><span class="language-css">    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;</span><br><span class="language-css">  &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="5-4-TodoHeader-vue"><a href="#5-4-TodoHeader-vue" class="headerlink" title="5.4 TodoHeader.vue"></a>5.4 TodoHeader.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;add&quot;</span>&gt;</span>待办事项<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;add&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;title&quot;</span> @<span class="hljs-attr">keydown.enter</span>=<span class="hljs-string">&quot;add&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;请输入任务标题, 敲回车确认&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;add&quot;</span>&gt;</span>添加<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-comment">// 第三方库 npm i nanoid</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> &#123;nanoid&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;nanoid&#x27;</span> </span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoHeader&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>) &#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> &#123; </span><br><span class="language-javascript">            <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">        <span class="hljs-comment">// 添加Todo</span></span><br><span class="language-javascript">        <span class="hljs-title function_">add</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">if</span>(!<span class="hljs-variable language_">this</span>.<span class="hljs-property">title</span>.<span class="hljs-title function_">trim</span>()) <span class="hljs-keyword">return</span> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;提示:输入的待办事项不能为空!&#x27;</span>)</span><br><span class="language-javascript">            <span class="hljs-comment">// 封装 Todo 对象</span></span><br><span class="language-javascript">            <span class="hljs-keyword">const</span> todoObj = &#123;</span><br><span class="language-javascript">                <span class="hljs-attr">id</span>: <span class="hljs-title function_">nanoid</span>(),</span><br><span class="language-javascript">                <span class="hljs-attr">title</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">title</span>,</span><br><span class="language-javascript">                <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span></span><br><span class="language-javascript">            &#125;</span><br><span class="language-javascript">            <span class="hljs-comment">// 调用回调函数</span></span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.$emit(<span class="hljs-string">&#x27;fnAddTodo&#x27;</span>, todoObj)</span><br><span class="language-javascript">            <span class="hljs-comment">// 清空</span></span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.<span class="hljs-property">title</span> = <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: gray;</span><br><span class="language-css">        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.2em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">button</span>, <span class="hljs-selector-tag">input</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">float</span>: right;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h2 id="5-5-TodoBody-vue"><a href="#5-5-TodoBody-vue" class="headerlink" title="5.5 TodoBody.vue"></a>5.5 TodoBody.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;todoObj in todoList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;todoObj.id&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">TodoItem</span> <span class="hljs-attr">:todoObj</span>=<span class="hljs-string">&quot;todoObj&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoItem</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoItem</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./TodoItem.vue&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoBody&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-attr">components</span>: &#123;<span class="hljs-title class_">TodoItem</span>&#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">props</span>: [<span class="hljs-string">&#x27;todoList&#x27;</span>]</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: pink;</span><br><span class="language-css">        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">ul</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">list-style</span>: none;</span><br><span class="language-css">        <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">0px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="5-6-TodoFooter-vue"><a href="#5-6-TodoFooter-vue" class="headerlink" title="5.6 TodoFooter.vue"></a>5.6 TodoFooter.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;totalTodoList&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;isAll&quot;</span>&gt;</span><br>        已完成 &#123;&#123; totalFinishedTodo &#125;&#125; / 全部 &#123;&#123; totalTodoList &#125;&#125;<br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;clearFinishedTodo&quot;</span>&gt;</span>清除已完成<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoFooter&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-attr">props</span>: [<span class="hljs-string">&#x27;todoList&#x27;</span>],</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">        <span class="hljs-title function_">clearFinishedTodo</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$emit(<span class="hljs-string">&#x27;fnClearFinishedTodo&#x27;</span>)</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 计算属性</span></span><br><span class="language-javascript">    <span class="hljs-attr">computed</span>:&#123;</span><br><span class="language-javascript">        <span class="hljs-comment">// 计算已完成的待办事项</span></span><br><span class="language-javascript">        <span class="hljs-title function_">totalFinishedTodo</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">reduce</span>(<span class="hljs-function">(<span class="hljs-params">pre, todoObj</span>) =&gt;</span> pre + (todoObj.<span class="hljs-property">done</span> ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>), <span class="hljs-number">0</span>) </span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">        <span class="hljs-comment">// 所有的待办事项</span></span><br><span class="language-javascript">        <span class="hljs-title function_">totalTodoList</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-property">length</span></span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">        <span class="hljs-attr">isAll</span>: &#123;</span><br><span class="language-javascript">            <span class="hljs-comment">// 全选按钮默认的勾选条件: 所有待办事项都已经完成</span></span><br><span class="language-javascript">            <span class="hljs-title function_">get</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">                <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">totalTodoList</span> &gt; <span class="hljs-number">0</span> &amp;&amp; <span class="hljs-variable language_">this</span>.<span class="hljs-property">totalFinishedTodo</span> == <span class="hljs-variable language_">this</span>.<span class="hljs-property">totalTodoList</span></span><br><span class="language-javascript">            &#125;,</span><br><span class="language-javascript">            <span class="hljs-title function_">set</span>(<span class="hljs-params">value</span>)&#123;</span><br><span class="language-javascript">                <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$emit(<span class="hljs-string">&#x27;fnUpdateCheckTodo&#x27;</span>,value)</span><br><span class="language-javascript">            &#125;</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: greenyellow;</span><br><span class="language-css">        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">input</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>: <span class="hljs-number">1.5em</span>;</span><br><span class="language-css">        <span class="hljs-attribute">width</span>: <span class="hljs-number">1.5em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">float</span><span class="hljs-selector-pseudo">:right</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h2 id="5-7-TodoItem-vue"><a href="#5-7-TodoItem-vue" class="headerlink" title="5.7 TodoItem.vue"></a>5.7 TodoItem.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">:checked</span>=<span class="hljs-string">&quot;todoObj.done&quot;</span> @<span class="hljs-attr">change</span>=<span class="hljs-string">&quot;handleCheckTodo(todoObj.id)&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;flag&quot;</span> <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;!todoObj.isEdit&quot;</span>&gt;</span>&#123;&#123; todoObj.title &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">transition</span> <span class="hljs-attr">appear</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">input</span></span><br><span class="hljs-tag">                <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;edit&quot;</span></span><br><span class="hljs-tag">                <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;todoObj.isEdit&quot;</span> </span><br><span class="hljs-tag">                <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;inputTodo&quot;</span></span><br><span class="hljs-tag">                <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;input come&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> </span><br><span class="hljs-tag">                <span class="hljs-attr">:value</span>=<span class="hljs-string">&quot;todoObj.title&quot;</span> </span><br><span class="hljs-tag">                @<span class="hljs-attr">blur</span>=<span class="hljs-string">&quot;handleSaveTodo(todoObj, $event)&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">transition</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;handleDeleteTodo(todoObj.id)&quot;</span>&gt;</span>删除<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;handleEditTodo(todoObj)&quot;</span>&gt;</span>编辑<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">&#x27;animate.css&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoItem&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">        <span class="hljs-title function_">handleCheckTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$emit(<span class="hljs-string">&#x27;fnCheckTodo&#x27;</span>, id)</span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">        <span class="hljs-title function_">handleDeleteTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">if</span>(<span class="hljs-title function_">confirm</span>(<span class="hljs-string">&#x27;确认要删除Id为 [&#x27;</span> + id + <span class="hljs-string">&#x27;] 的待办事项吗?&#x27;</span>))</span><br><span class="language-javascript">                <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$emit(<span class="hljs-string">&#x27;fnDeleteTodo&#x27;</span>, id)</span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">        <span class="hljs-title function_">handleEditTodo</span>(<span class="hljs-params">todoObj</span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-comment">// 判断待办事项是否有isEdit属性</span></span><br><span class="language-javascript">            <span class="hljs-keyword">if</span>(!<span class="hljs-title class_">Object</span>.<span class="hljs-title function_">hasOwn</span>(todoObj, <span class="hljs-string">&#x27;isEdit&#x27;</span>))&#123;</span><br><span class="language-javascript">                <span class="hljs-comment">// 若没有则动态设置为true</span></span><br><span class="language-javascript">                <span class="hljs-variable language_">this</span>.$set(todoObj, <span class="hljs-string">&#x27;isEdit&#x27;</span>, <span class="hljs-literal">true</span>)</span><br><span class="language-javascript">            &#125; <span class="hljs-keyword">else</span>&#123;</span><br><span class="language-javascript">                <span class="hljs-comment">// 若有则直接设置为true</span></span><br><span class="language-javascript">                todoObj.<span class="hljs-property">isEdit</span> = <span class="hljs-literal">true</span></span><br><span class="language-javascript">            &#125;</span><br><span class="language-javascript">            <span class="hljs-comment">// 聚焦到input, 调用$nextTick, 等页面渲染后才执行回调函数</span></span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.$nextTick(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">                <span class="hljs-variable language_">this</span>.<span class="hljs-property">$refs</span>.<span class="hljs-property">inputTodo</span>.<span class="hljs-title function_">focus</span>()</span><br><span class="language-javascript">            &#125;)</span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">        <span class="hljs-title function_">handleSaveTodo</span>(<span class="hljs-params">todoObj, event</span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-comment">// 取消编辑</span></span><br><span class="language-javascript">            todoObj.<span class="hljs-property">isEdit</span> = <span class="hljs-literal">false</span></span><br><span class="language-javascript">            <span class="hljs-keyword">if</span>(!event.<span class="hljs-property">target</span>.<span class="hljs-property">value</span>.<span class="hljs-title function_">trim</span>()) <span class="hljs-keyword">return</span> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;事项内容不能为空!&#x27;</span>)</span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$emit(<span class="hljs-string">&#x27;fnUpdateTodo&#x27;</span>, todoObj.<span class="hljs-property">id</span>, event.<span class="hljs-property">target</span>.<span class="hljs-property">value</span>)</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 引入Body传来的todoObj 和 App传来的 fnCheckTodo方法</span></span><br><span class="language-javascript">    <span class="hljs-attr">props</span>: [<span class="hljs-string">&#x27;todoObj&#x27;</span>],</span><br><span class="language-javascript"></span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">196</span>, <span class="hljs-number">100</span>, <span class="hljs-number">100</span>);</span><br><span class="language-css">        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">float</span><span class="hljs-selector-pseudo">:right</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">        <span class="hljs-attribute">display</span>: none;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">input</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">width</span>:<span class="hljs-number">1.5em</span>;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">1.5em</span></span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.input</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>:<span class="hljs-number">1.1em</span>;</span><br><span class="language-css">        <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span><span class="hljs-selector-pseudo">:hover</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">31</span>, <span class="hljs-number">101</span>, <span class="hljs-number">167</span>);</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span><span class="hljs-selector-pseudo">:hover</span> <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">display</span>: block;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.come</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">animation</span>: fromRight linear <span class="hljs-number">1s</span>;</span><br><span class="language-css">    &#125;    </span><br><span class="language-css">    <span class="hljs-selector-class">.to</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">animation</span>: fromRight linear <span class="hljs-number">1s</span> reverse;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-comment">/* Vue自动指定name为edit和flag的进入和进出样式 */</span></span><br><span class="language-css">    <span class="hljs-selector-class">.edit-enter-active</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">animation</span>: fromRight linear <span class="hljs-number">1s</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.edit-leave-active</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">animation</span>: fromRight linear <span class="hljs-number">1s</span> reverse;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-comment">/* Vue 动画 */</span></span><br><span class="language-css">    <span class="hljs-keyword">@keyframes</span> fromRight&#123;</span><br><span class="language-css">        <span class="hljs-selector-tag">from</span>&#123;</span><br><span class="language-css">            <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(<span class="hljs-number">100px</span>);</span><br><span class="language-css">            <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0</span>;</span><br><span class="language-css">        &#125;</span><br><span class="language-css">        <span class="hljs-selector-tag">to</span>&#123;</span><br><span class="language-css">            <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(<span class="hljs-number">0px</span>);</span><br><span class="language-css">            <span class="hljs-attribute">opacity</span>: <span class="hljs-number">100</span>;</span><br><span class="language-css">        &#125;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><br></code></pre></td></tr></table></figure>

<h1 id="六、总结"><a href="#六、总结" class="headerlink" title="六、总结"></a>六、总结</h1><hr>
<p>这两天通过一个TodoList 待办事项的案例对Vue2的学习进行了拓展，个人感觉Vue开发非常方便，而且逻辑层次分的很清楚，不会像之前那样，HTML和JS都杂糅在一起，看起来很混乱，现在使用Vue将不同的部分划分为不同的组件，而每一个组件中都有相应的 HTML、JS和CSS，非常方便。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%89%8D%E7%AB%AF/" class="category-chain-item">前端</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Vue/">#Vue</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象</div>
      <div>http://example.com/2022/06/21/Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年6月21日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/06/24/Vue2%20%E7%AC%94%E8%AE%B04%20%E5%9F%BA%E4%BA%8EVue2%E7%9A%84%E5%AD%A6%E7%94%9F%E4%BF%A1%E6%81%AF%E5%A2%9E%E5%88%A0%E6%9F%A5%E6%A1%88%E4%BE%8B%20_%20Vuex%E7%BB%84%E4%BB%B6%E7%AE%80%E4%BB%8B%E3%80%81%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%20_%20Vuex%E7%9A%84%E4%B8%89%E5%B1%82%E7%BB%93%E6%9E%84%E2%80%94%E2%80%94store%E8%B4%9F%E8%B4%A3%E5%93%8D%E5%BA%94%E7%BB%84%E4%BB%B6%E3%80%81mutations%E8%B4%9F%E8%B4%A3%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E3%80%81state%E8%B4%9F%E8%B4%A3%E5%AD%98%E5%82%A8%E6%95%B0%E6%8D%AE/" title="Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/06/21/Vue2%20%E7%AC%94%E8%AE%B01%20%E5%9F%BA%E4%BA%8EVue2%E7%9A%84TodoList%E5%BE%85%E5%8A%9E%E4%BA%8B%E9%A1%B9%E5%A2%9E%E5%88%A0%E6%9F%A5%E6%A1%88%E4%BE%8B%20_%20%E5%9F%BA%E4%BA%8ENode.js%20%E4%BD%BF%E7%94%A8Vue-CLI%E8%84%9A%E6%89%8B%E6%9E%B6%E6%9E%84%E5%BB%BA%E9%A1%B9%E7%9B%AE/" title="Vue2 笔记1 基于Vue2的TodoList待办事项增删查案例 _ 基于Node.js 使用Vue-CLI脚手架构建项目">
                        <span class="hidden-mobile">Vue2 笔记1 基于Vue2的TodoList待办事项增删查案例 _ 基于Node.js 使用Vue-CLI脚手架构建项目</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
